<template>
	<view>
		<view class="content">
			<u-checkbox-group shape="circle" size="60" :wrap="true" width="100%">
				<u-checkbox v-model="item.checked" v-for="(item, index) in listClasses" :key="item.id" :name="item.id">
					{{item.myClass}}
				</u-checkbox>
			</u-checkbox-group>
			<view class="btn">
				<u-button @click="checkedAll()" :ripple="true" shape="circle" size="medium">全选</u-button>
				<u-button type="success" @click="submit()" :ripple="true" shape="circle" size="medium">确认<u-icon
						name="checkbox-mark"></u-icon>
				</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				listClasses: [],

				selectIds: [],

				eventChannel: null,
			}
		},
		onLoad(option) {
			var that = this
			// #ifdef APP-NVUE
			this.eventChannel = this.$scope.eventChannel; // 兼容APP-NVUE
			// #endif
			// #ifndef APP-NVUE
			this.eventChannel = this.getOpenerEventChannel();
			// #endif
			// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
			this.eventChannel.on('acceptDataFromOpenerPage', function(data) {
				that.selectIds = data.addressee.map(item => {
					return item.id
				})
			})
			this.getClasses()
		},
		methods: {
			checkedAll() {
				this.listClasses.map(val => {
					val.checked = true;
				})
			},
			async getClasses() {
				const res = await this.$myRequest({
					url: "/teacher/listClasses"
				})
				this.listClasses = res.data.data.map(c => {
					return {
						id: c.classId,
						myClass: c.myClass.grade.year + "" + c.myClass.grade.name + "" + c.myClass.name,
						checked: this.selectIds.indexOf(c.classId) != -1
					}
				})
				// this.addressee = this.$u.deepClone(this.listClasses);
				// console.log(this.listClasses)
			},
			submit() {
				let addressee = this.listClasses.filter(c => c.checked)
				this.eventChannel.emit('acceptDataFromOpenedPage', {
					addressee: addressee
				});
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background-color: #fff;
		padding: 10rpx 15rpx;

		/deep/.u-checkbox {
			padding: 5rpx;
			margin-bottom: 10rpx;
			border-bottom: 1px solid #eee;
		}

		.btn {
			display: flex;
			justify-content: space-between;
		}
	}
</style>
